<?php
class PNModel extends PNObjectEx {
  function PNModel($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_model';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_init($init, $where);
  }

  function validate(){
    $form = FormUtil::getPassedValue ('form', false);
    if (empty($form['name'])){
      $message[] = 'field name must not be empty.';
      $this->setError($message);
      return false;
    }
    $this->clearError();
    return true;
  }

  function insertPostProcess(){
    $brand_id = $this->_objData['brand_id'];
    $sb = FormUtil::getPassedValue ('sb', false);
    $form = FormUtil::getPassedValue ('form', false);
    $id = $this->_objData['id'];
    if($sb && $id){
      foreach($sb as $key=>$val ){
        //$valArray = explode("@", $key);
        $obj[] = array('category_id'    => $val,
                       'subcategory_id' => $key,
                       'model_id'       => $id,
                       'brand_id'       => $brand_id
                      );
      }
      //Do the insert
      DBUtil::insertObjectArray($obj, 'innoforum_subcategory_model');
    }

  }

  function updatePostProcess(){

    $id = $this->_objData['id'];
    $brand_id = $this->_objData['brand_id'];
    $sbFormArray = FormUtil::getPassedValue ('sb', false);

    //Delete old data 
    DBUtil::deleteObjectByID( 'innoforum_subcategory_model', $id, 'model_id');

    //Insert new data
    foreach($sbFormArray as $keyForm=>$valForm ){
      $obj = array('category_id'    => $valForm,
                       'subcategory_id' => $keyForm,
                       'model_id'       => $id,
                       'brand_id'       => $brand_id
                      );
      //Do the insert
      DBUtil::insertObject($obj, 'innoforum_subcategory_model');
    }
  }


  function deletePostProcess(){
    $id = $this->_objData['id'];
    //Delete old data 
    DBUtil::deleteObjectByID('innoforum_subcategory_model', $id, 'model_id');
  }

  function selectExtendResult(){
    $id = FormUtil::getPassedValue ('id', false);
    $result = array();
    if ($id){

      $result['subcategory_model'] = DBUtil::selectObjectArray( 'innoforum_subcategory_model', 
                                                                "WHERE smod_model_id = '$id'",
                                                                '',
                                                                -1,
                                                                -1,
                                                                '',
                                                                null,
                                                                null,
                                                                array( 'id', 
                                                                      'category_id', 
                                                                      'subcategory_id', 
                                                                      'model_id',
                                                                      'brand_id'
                                                                    )
                                                                );

    }
    return $result;
  }

}